<?php
/**
 * Created by PhpStorm.
 * User: gakuin
 * Date: 7/8/16
 * Time: 4:03 PM
 */
namespace app\controllers;

use yii;
use yii\web\Controller;
use yii\web\CookieCollection;
use yii\web\Cookie;
use yii\web\Request;
use app\models\AdminUser;
use app\models\AdminLoginForm;
use yii\web\Session;
use app\models\User;
use app\models\LoginForm;

class BackstageController extends  BackbaseController
{

    public function actionLoginview(){

        $view = Yii::$app->view;
        $view->params['menuitemid'] = 11;
        return $this -> privateLogin();

    }


    // 登录 兼 首页
    public function privateLogin()
    {

        $model = new AdminLoginForm();

        // 获取cookie
        if (!isset($_COOKIE["fxusername"])) {// 获取cookie 失败

            $model->load(yii::$app->request->post());
            if ($model->username) {

                $returndata = $this->actionLogin($model->username, $model->password);
                if ($returndata['code'] == 1 && $returndata['status']['type'] != 0) {

                    if ($model->rememberMe) {
                        setcookie("fxusername", $model->username, time() + (3600 * 24 * 10),'/');
                        setcookie("fxpassword", $model->password, time() + (3600 * 24 * 10),'/');
                        setcookie("fxuserid",$returndata['status']['user_id'],time() + (3600 * 24 * 10),'/');
                        setcookie('fxtype',$returndata['status']['type'],time() + (3600 * 24 * 10),'/');
                    }
                    if($returndata['status']['type']  == 2 ){

//                       商家登录成功
                        $path =  yii::$app -> request ->baseUrl;
                        return $this -> redirect($path.'/index.php/publish/index');

                    } else if($returndata['status']['type']  == 1){

//                       代理商登录成功
                        $path =   yii::$app -> request ->baseUrl;
                        return $this -> redirect( $path .'/index.php/lowgrade/mybroke');
                    }

                }else{

                    $model -> error = true;
                    return $this -> renderPartial('loginview',['model' => $model]);
                }

            } else {
                $model -> error = false;
                return $this->renderPartial('loginview', ['model' => $model]);
            }
        } else {// 获取cookie成功

            $this -> loginviewCookie();
        }

    }


    public  function loginviewCookie(){

        if($_COOKIE['fxtype']  == 2 ){
//                       商家登录成功
            $path =  yii::$app -> request ->baseUrl;
            return $this -> redirect($path.'/index.php/publish/index');
        } else if($_COOKIE['fxtype']  == 1){

//                       代理商登录成功
            $path =  yii::$app -> request ->baseUrl;
            return $this -> redirect($path.'/index.php/lowgrade/mybroke');
        }else{
            $this -> actionLoginoutview();
        }

    }

    public function actionLogin($username,$password){


        $user = AdminUser::find() -> where(['username'=>$username,'password'=>$password,'del_flag' => '1'])->asArray()->one();

        if($user){
            return['code'=>1,
                'status' => $user
            ];
        }else{

            $rootuser = AdminUser::find() -> where(['username'=>$username,'del_flag' => '1'])->asArray()->one();
            if(strlen($rootuser['password']) < 15){
                return ['code' => 0];
            }else{
                $model = new LoginForm();
                $model->username = $username;
                $model->password = $password;

                if($model->validate()){

                    $md5user = AdminUser::find() -> where(['username'=>$username,'del_flag' => '1'])->asArray()->one();
                    if (Yii::$app->getSecurity()->validatePassword($password, $md5user['password'])) {

                        return['code'=>1,
                            'status' => $md5user,
                        ];
                    } else {
                        return ['code'=>0];
                    }
                }else{

                    return ['code' => 0];
                }
            }

        }
    }

    // 退出登录
    public function actionLoginoutview(){

        setcookie("fxusername",FALSE,time() - 600,'/');
        setcookie("fxpassword",FALSE,time() - 600,'/');
        setcookie("fxuserid",FALSE,time() - 600,'/');
        setcookie("fxtype",FALSE,time() - 600,'/');

        $path =  yii::$app -> request ->baseUrl;
        return $this -> redirect($path.'/index.php/backstage/loginview');
    }

    // 报表数据
    public function actionStatementform(){

        $view = Yii::$app->view;
        $view->params['menuitemid'] = 11;
        $this -> render('loginview');

    }
}

?>